﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.StorageClient.Protocol;
using AzureGames.Model;
using System.Collections.Specialized;

using AzureGames.Data.Implementation.ServiceEntities;

namespace AzureGames.Data.Implementation
{
    internal class GameResourceRepository : AzureTableRepository<GameResourceEntity>
    {
        const string PARTITIONKEY = "GameResource";

        public GameResourceRepository() : base("GameResource") { }

        public GameResourceEntity GetGameResource(Guid resourceID)
        {
            return Get(PARTITIONKEY, resourceID.ToString());
        }

        public List<Guid> GetGameResources(int gameID, string resourceType)
        {
            // TODO: why the "Can only specify query options (orderby, where, take, skip) after last navigation." when performed in one step
            var resources = dataContext.CreateQuery<GameResourceEntity>(this.tableName)
                .Where(n=> n.PartitionKey==PARTITIONKEY && n.GameID==gameID && n.ResourceType == resourceType);

            var x = resources.ToList();

            var l = x.OrderBy(n => n.ResourceOrder).Select(n => n.ID).ToList();

            return l;
        }

        public AzureResult AddGameResource(GameResourceEntity resource)
        {
            Add(resource);

            return new AzureResult { Code = 0 };
        }

        public AzureResult UpdateGameDefinition(GameResourceEntity resource)
        {
            Update(resource);

            return new AzureResult { Code = 0 };
        }
        
      

    }
}
